package cn.com.generaldata.os.service.indicator_evaluation.incr_analysis;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 地区生产总值平减指数评估指标计算类.
 */
public class GDPDeflatorCalculateService extends IndicatorEvaluationCalculateService {

	/** 定期年份 */
	private int baseYear = 2001;
	/** 地区生产总值 */
	private String GDPComparablePriceIndicatorName = "地区生产总值可比价";

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateProvinceResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 市指标没确定先保留
		}
		return result;
	}

	private Double calculateProvinceResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		// 令基期2001年等于1
		if (indicatorAccountVO.getYear() == baseYear) {
			return 1d;
		}

		Double GDP = getInputOrAccountingIndicatorValue(indicatorAccountVO, indicatorAccountVO.getYear(),
				ProvinceIndicatorConstant.GDP_INDICATOR_NAME);
		Validate.isTrue(GDP != null);

		Double GDPComparablePrice = getGDPComparablePrice(indicatorAccountVO);
		Validate.isTrue(GDPComparablePrice != null, "海洋生产总值可以比价为0，不可以计算");
		return GDP / GDPComparablePrice;
	}

	/**
	 * 获得地区生产总值可比价
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getGDPComparablePrice(IndicatorAccountVO indicatorAccountVO) {
		Integer indicatorId = getIndicatorEvaluationId(GDPComparablePriceIndicatorName, indicatorAccountVO.getAreaId());
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), indicatorId);
		return getEvaluationIndicatorValue(cri);
	}
}
